import javax.swing.JPanel;
import java.awt.*;

public class TetrisPanel extends JPanel {
    public static final int CELL_SIZE = 30;
    private Tetris tetris;

    public TetrisPanel(Tetris tetris) {
        this.tetris = tetris;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawBoard(g);
        drawTetromino(g, tetris.getCurrentTetromino());
        drawScore(g);
    }

    private void drawTetromino(Graphics g, Tetromino tetromino) {
        int[][] shape = tetromino.getShape();
        int x = tetromino.getX();
        int y = tetromino.getY();

        for (int row = 0; row < shape.length; row++) {
            for (int col = 0; col < shape[row].length; col++) {
                if (shape[row][col] != 0) {
                    g.setColor(Color.BLUE);
                    g.fillRect((x + col) * CELL_SIZE, (y-1 + row) * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    g.setColor(Color.BLACK);
                    g.drawRect((x + col) * CELL_SIZE, (y-1 + row) * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                }
            }
        }
    }

    private void drawBoard(Graphics g) {
        for (int row = 0; row < tetris.getBoard().getHeight(); row++) {
            for (int col = 0; col < tetris.getBoard().getWidth(); col++) {
                int value = tetris.getBoard().getCell(col, row);
                if (value != 0) {
                    g.setColor(Color.BLUE);
                    g.fillRect(col * CELL_SIZE, (row-1) * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    g.setColor(Color.BLACK);
                    g.drawRect(col * CELL_SIZE, (row-1) * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                }
            }
        }
    }

    private void drawScore(Graphics g) {
        g.setColor(Color.BLACK);
        g.setFont(new Font("Tahoma", Font.BOLD, 14));
        g.drawString("Score: " + tetris.getScore(), 10, 20);
    }
}
